using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using MM.Entities;

namespace MM.Business
{
    class clsNHA_SAN_XUAT
    {
        public clsNHA_SAN_XUAT(string sConn)
	    {
		    SqlHelper.ConnectionString = sConn;
	    }
        public List<NHA_SAN_XUAT> DataTableToLstObj(DataTable dt)
        {
            NHA_SAN_XUAT nsxDTO;
            List<NHA_SAN_XUAT> lstNsxDTO = new List<NHA_SAN_XUAT>();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                nsxDTO = new NHA_SAN_XUAT();
                try
                {
                    nsxDTO._maNhaSanXuat = dt.Rows[i]["MaNhaSanXuat"].ToString();
                }
                catch (Exception ex)
                {
                }
                try
                {
                    nsxDTO._tenNhaSanXuat = dt.Rows[i]["TenNhaSanXuat"].ToString();
                }
                catch (Exception ex)
                {
                }
                lstNsxDTO.Add(nsxDTO);
            }
            return lstNsxDTO;
        }
        public NHA_SAN_XUAT DataTableToObj(DataTable dt)
        {
            NHA_SAN_XUAT nsxDTO = new NHA_SAN_XUAT();
            try
            {
                nsxDTO._maNhaSanXuat = dt.Rows[0]["MaNhaSanXuat"].ToString();
            }
            catch (Exception ex)
            {
            }
            try
            {
                nsxDTO._tenNhaSanXuat = dt.Rows[0]["TenNhaSanXuat"].ToString();
            }
            catch (Exception ex)
            {
            }
            return nsxDTO;
        }
        public List<NHA_SAN_XUAT> LoadNhaSanXuat()
        {
            DataTable dt = new DataTable();
            List<NHA_SAN_XUAT> lstNsxDTO = new List<NHA_SAN_XUAT>();
            try
            {
                SqlHelper.OpenConnection();
                string spName = "usp_SelectNHA_SAN_XUATsAll";
                dt = SqlHelper.ExecuteToDataTable(spName, CommandType.StoredProcedure);
                lstNsxDTO = DataTableToLstObj(dt);
            }
            catch (Exception ex)
            {
            }
            finally
            {
                if (!SqlHelper.IsHaveTran())
                    SqlHelper.CloseConnection();
            }
            return lstNsxDTO;
        }
        public NHA_SAN_XUAT SelectNHA_SAN_XUATByID(string id, string maTau)
        {
            DataTable dt = new DataTable();
            NHA_SAN_XUAT nsxDTO = new NHA_SAN_XUAT();
            try
            {
                SqlHelper.OpenConnection();
                string spName = "usp_SelectNHA_SAN_XUATByID";
                SqlParameter[] par = new SqlParameter[2];
                par[0] = new SqlParameter("@MaVTPT", id);
                par[1] = new SqlParameter("@MaTau", maTau);
                //dt = SqlHelper.ExecuteToDataTable(spName, CommandType.Text);
                //lst = GetVYLUONG_KHOANLUONG(dt);
                dt = SqlHelper.ExecuteToDataTable(spName, par);
                nsxDTO = DataTableToObj(dt);
            }
            catch (Exception ex)
            {
            }
            finally
            {
                if (!SqlHelper.IsHaveTran())
                    SqlHelper.CloseConnection();
            }
            return nsxDTO;
        }
        public int InsertUpdateNHA_SAN_XUAT(string maNhaSanXuat, string tenNhaSanXuat)
        {
            int result = 0;
            try
            {
                SqlHelper.OpenConnection();
                string spName = "usp_InsertUpdateNHA_SAN_XUAT";
                SqlParameter[] par = new SqlParameter[2];
                par[0] = new SqlParameter("@MaNhaSanXuat", maNhaSanXuat);
                par[1] = new SqlParameter("@TenNhaSanXuat", tenNhaSanXuat);

                result = SqlHelper.ExecuteNonQuery(spName, par);
            }
            catch (Exception ex)
            {
            }
            finally
            {
                if (!SqlHelper.IsHaveTran())
                    SqlHelper.CloseConnection();
            }
            return result;
        }
    }
}
